var config = require("../../config");

Page({
  data: {},
  onLoad() {
  },
  onShareAppMessage() {

  },
  onShow() {
    let _this = this;
    wx.getNetworkType({
      success: function (res) {
        if (res.networkType != "none") {
          wx.showToast({
            title: '加载中...',
            icon: 'loading',
            mask: true
          });
          wx.request({
            method: 'POST',
            url: config.service.addressList,
            data:{  
              uid: wx.getStorageSync("loginInfo").uid,
              logintoken: wx.getStorageSync("loginInfo").token
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
              console.log("我的地址:",res);
              wx.hideToast();
              if (res.data.code == 1) {
                _this.setData({
                  siteList: res.data.data
                });
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'loading',
                  mask: true
                });
              }
            }
          });
        } else {
          wx.showToast({
            title: '好像没网了...',
            icon: 'loadgin',
            mask: true
          });
        }
      },
    });
  },
  address(e) {
    let index = e.currentTarget.dataset.index;
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];  //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面
    this.data.siteList[index].type = 1;
    prevPage.onShow(this.data.siteList[index]);
    this.setData({
      selected: index
    });
    setTimeout(function () {
      wx.navigateBack();
    }, 1000);
  },
  remove(e) {
    let index = e.currentTarget.dataset.index, _this = this;
    wx.showModal({
      title: '提示',
      content: '您要删除地址吗',
      success: function (e) {
        if (e.confirm) {
          wx.getNetworkType({
            success: function (res) {
              if (res.networkType != "none") {

                wx.showLoading({
                  title: "删除中...",
                  mask: true
                });

                wx.request({
                  method: 'POST',
                  url: config.service.deleteAddress,
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  data: {
                    uid: wx.getStorageSync("loginInfo").uid,
                    logintoken: wx.getStorageSync("loginInfo").token,
                    id: _this.data.siteList[index].id,
                  },
                  success(res) {
                    wx.hideLoading();
                    if (res.data.code == 1) {
                      _this.onShow();
                      wx.showToast({
                        title: res.data.msg,
                        icon: 'loading',
                        duration: 2000,
                        mask: true
                      });
                    } else {
                      wx.showToast({
                        title: res.data.msg,
                        icon: 'loading',
                        duration: 2000,
                        mask: true
                      });
                    }
                  }
                });
              } else {
                wx.showToast({
                  title: '好像没网了...',
                  icon: 'loading',
                  duration: 2000,
                  mask: true
                });
              }
            },
          })
        }
      }
    });
  },
  addAddress() {
    wx.navigateTo({
      url: '../addAddress/addAddress',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
});